<?php

namespace App;

use Cache;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
    use Authenticatable, CanResetPassword;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'admin';

    public $timestamps = false;

    protected $primaryKey = 'id';

    public function role()
    {
        return $this->hasOne('App\Role','current_role','id');
    }

    /**
     * @param int $cache
     * @return bool
     */
    static function getPermissionNav($cache = 0)
    {
        $submodules = Cache::has('submodules') ? Cache::get('submodules') : false;
        if($submodules === false || $cache ) {
            $data = array();
            $user = Auth::user();
            $user_role = DB::table('adm_role_user as aru')->join('adm_role as ar', 'aru.role_id', "=", "ar.id")->join('admin', 'aru.admin_id', '=', 'admin.id')->select("ar.*")->where(
                'aru.admin_id',
                $user->id
            )->first();
            if ($user_role->type == 'System') {
                $submodules = DB::table('adm_submodule')->where('status', 1)->orderBy('sort', 'asc')->get();
            } else {
                $submoduels = DB::table('admin_role_submodule as ars')
                    ->join('admin_submodule as as', 'ars.submodule_id', '=', 'as.id')
                    ->select('as.*')
                    ->where('ars.role_id', $user_role->id)->where('as.status', 1)
                    ->orderBy('as.sort', 'asc')
                    ->get();
            }
            foreach ($submodules as $submodule) {
                $data[$submodule->id]['id'] = $submodule->id;
                $data[$submodule->id]['name'] = $submodule->name;
                $data[$submodule->id]['code'] = $submodule->code;
                $data[$submodule->id]['parent_id'] = $submodule->pid;
                $data[$submodule->id]['url'] = action($submodule->code, '@getIndex', ['submodule_id' => $submodule->id]);

            }
            $data = toTree($data, 'id');
            $submodules = $data;
            Cache::add('submoduels');
        }
        return $submodules;
    }
    static function getPermissionAction($controller_id,$cache=0)
    {
        $actions = Cache::has('actions') ? Cache::get('actions'):false;
        if($actions === false || $cache)
        {
            $data = array();
            $user = Auth::user();
            $user_role = DB::table('adm_role_user as aru')->join("adm_role as ar",'aru.role_id',"=","ar.id")->join('admin','aru.admin_id','=','admin.id')->select("ar.*")->where(
                'aru.admin_id',
                $user->id
            )->first();
            if($user_role->type == 'System'){
                $actions = DB::table('adm_action')->where('status',1)->where('controller_id',$controller_id)->orderBy('sort','asc')->get();
            }else{
                $actions = DB::table('adm_role_action as ara')
                    ->join('adm_action as aa','ara.action_id','=','aa.id')
                    ->select('aa.*')
                    ->where('ara.role_id',$user_role->id)->where('aa.controller_id',$controller_id)->where('aa.status',1)
                    ->orderBy('aa.sort','asc')
                    ->get();
            }
            foreach($actions as $action)
            {
                $data[] = $action->code;
            }
            $actions = $data;
            Cache::add('actions',$data,86400);
        }
        return $actions;
    }
    static function loginSuccess($user)
    {
        Auth::loginUsingId($user->id);
        $user->last_ip = getIP();
        $user->login_count += 1;
        $user->save();
        $user_role = DB::table('adm_role_user as aru')->join("adm_role as ar", "aru.role_id","=","ar.id")->join('admin','aru.admin_id','=','admin.id')->select("ar.*")->where(
            'aru.admin_id',
            $user->id
        )->first();
        session(['user_role'=>$user_role]);
    }
}
